package com.mmlc.meimei.lichengkongjian.Adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.MyLoveResponseBean;

import org.xutils.x;

import java.util.List;

public class MyLoveAdapter extends RecyclerView.Adapter{
    List<MyLoveResponseBean.DataBean> data;
    Context context;
    boolean isShowCheck=false;

    public MyLoveAdapter(Context context, List<MyLoveResponseBean.DataBean> item){
        this.context = context;
        this.data = item;
    }

    public boolean isShowCheck() {
        return isShowCheck;
    }

    public void setShowCheck(boolean showCheck) {
        isShowCheck = showCheck;
    }

    private FastBuyAdapter.OnItemClickListener clickListener;

    public void setClickListener(FastBuyAdapter.OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_mylove, parent, false);
        LoveVh vh = new LoveVh(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof  LoveVh) {
            final LoveVh vh = (LoveVh) holder;
            x.image().bind(vh.iv_photo, data.get(position).getImg_thumb());
            vh.tv_shopDesc.setText(data.get(position).getGoods_name());
            vh.tv_price.setText("¥ " + data.get(position).getPrice());
            vh.checkBox.setChecked(data.get(position).isCheck());
            if (position == data.size() - 1) {
                vh.line.setVisibility(View.GONE);
            } else {
                vh.line.setVisibility(View.VISIBLE);
            }
            if(isShowCheck){
                vh.checkBox.setVisibility(View.VISIBLE);
            }else{
                vh.checkBox.setVisibility(View.GONE);
            }
            vh.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    data.get(position).setCheck(b);
                }
            });

            vh.rl_love.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getLayoutPosition(); // 1
                    clickListener.onClick(vh.rl_love,position); // 2
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class LoveVh extends RecyclerView.ViewHolder{
        ImageView iv_photo;
        TextView tv_shopDesc,tv_price;
        View line;
        RelativeLayout rl_love;
        CheckBox checkBox;
        public LoveVh(View itemView) {
            super(itemView);
            iv_photo = (ImageView) itemView.findViewById(R.id.iv_photo);

            checkBox = (CheckBox) itemView.findViewById(R.id.check);

            tv_shopDesc = (TextView) itemView.findViewById(R.id.tv_tilteDesc);

            tv_price = (TextView) itemView.findViewById(R.id.tv_price);

            line = itemView.findViewById(R.id.line);

            rl_love = (RelativeLayout) itemView.findViewById(R.id.rl_love);
        }
    }

}


